//
//  ChooseTireViewController.m
//  大轮胎
//
//  Created by 朱永超 on 2017/3/16.
//  Copyright © 2017年 zyc. All rights reserved.
//

//第一个
//http://lscy4.caeac.com.cn/api/product_fenlei.php?appkey=1138

//第二个
//http://lscy4.caeac.com.cn/api/product.php?appkey=1138&tid=43&sort=0&page=0

#define URU_TABLE @"http://lscy4.caeac.com.cn/api/product_fenlei.php?appkey=1138"
#import "ChooseTireViewController.h"
#import "ChooseCollectionViewCell.h"
#import "ChooseCollectionFlowLayout.h"
#import "YCNetWork.h"
#import "Choosemodel.h"
#import "ChooseContollerModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "DetailsViewController.h"
@interface ChooseTireViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
{
    ChooseTireView*choose;
    //传递表格数据
    NSMutableArray*tablArray;
    //传递ID值
    NSMutableArray *strid;
    //传递网格数据
    NSMutableArray*contollectArray;
    NSString *str;
    
    NSMutableArray *ids;
    
    DetailsViewController*det;
    
}

@end

@implementation ChooseTireViewController

-(void)viewWillAppear:(BOOL)animated
{
    [self tableshuju];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    strid = [[NSMutableArray alloc]init];
    //设置标题
    self.navigationItem.title = @"轮胎商城";
    //设置导航标题字体大小和颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],
    NSForegroundColorAttributeName:[UIColor whiteColor]}];

    //设置背景颜色
    self.view.backgroundColor = [UIColor yellowColor];
    
    
    //初始化chooseview
    
    choose = [[ChooseTireView alloc]init];
    choose.collection.backgroundColor = [UIColor whiteColor];
    
    self.view = choose;
    
    //表格协议
    choose.table.delegate=self;
    choose.table.dataSource=self;
    
    //网格协议
    choose.collection.dataSource=self;
    choose.collection.delegate=self;
    
    [choose.collection registerClass:[ChooseCollectionViewCell class] forCellWithReuseIdentifier:@"Mycell"];
    
    //不显示下划线
    
    choose.table.tableFooterView=[[UIView alloc]init];
    
    
    //表格数据
    [self tableshuju];
    //网格数据
    [self collextionshuaxin];
    
    
    
     }
//解析表格数据
-(void)tableshuju
{
    //解析表格数据
    
    [YCNetWork postWithURLString:URU_TABLE parameters:nil success:^(id data) {
        
        tablArray = [NSMutableArray array];
        
        for (NSDictionary*dic in data) {
            
            Choosemodel*model = [[Choosemodel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            [tablArray addObject:model];
            
        }
        
        [choose.table reloadData];
        
        //设置默认点击事件需要放在刷新表格之后
        NSIndexPath *idxPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [choose.table selectRowAtIndexPath:idxPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
}
//解析网格数据
-(void)collextionshuaxin{
    NSString*UrlCollect = [NSString stringWithFormat:@"http://lscy4.caeac.com.cn/api/product.php?appkey=1138&tid=%@&sort=0&page=0",str];
    
    [YCNetWork postWithURLString:UrlCollect parameters:nil success:^(id data) {
        
        ChooseContollerModel*mode=[[ChooseContollerModel alloc]init];
        
        [mode setValuesForKeysWithDictionary:data];
        
        contollectArray=[NSMutableArray array];
        
        
        ids=[NSMutableArray array];
        //forin加载字典
        for (NSDictionary*dic in mode.product) {
            ChooseContollerModell*ll=[[ChooseContollerModell alloc]init];
            
            [ll setValuesForKeysWithDictionary:dic];
            [contollectArray addObject:ll];
        
            
            [ids addObject:ll];
            
        }
        //刷新网格
        [choose.collection reloadData];
        
    } failure:^(NSError *error) {
    }];
}

//网格单元格多少
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return contollectArray.count;
}

//网格内容
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseCollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Mycell" forIndexPath:indexPath];
    
    ChooseContollerModell*mo=contollectArray[indexPath.row];
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:mo.img] placeholderImage:[UIImage imageNamed:@"2"]];
    cell.lable.text=mo.name;
    
    cell.pricelabel.text = [NSString stringWithFormat:@"¥ %@",mo.price];
    
    NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@",mo.price_yj]];
    [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
   cell.price.attributedText = newPrice;
    
    cell.layer.borderColor=[UIColor darkGrayColor].CGColor;
    
    cell.layer.borderWidth=0.3;

    
    return cell;
}
//网格点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    det=[[DetailsViewController alloc]init];
    
    ChooseContollerModell *ll = ids[indexPath.item];
    
    det.idss= ll.ids;
    
    NSLog(@"%@",ids);
   
    [self.navigationController pushViewController:det animated:NO];
    
    [self collextionshuaxin];
}

//表格方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tablArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString*cellid=@"cell";
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
    }
    //添加数据
    Choosemodel*mo=tablArray[indexPath.row];
    cell.textLabel.text=mo.name;
    cell.textLabel.font=[UIFont systemFontOfSize:12];
    
    //添加边框cell
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [[UIColor alloc] initWithRed:0.888 green:0.986 blue:0.590 alpha:1.000];
    
    return cell;
    
}
//表格点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Choosemodel *ll = tablArray[indexPath.row];
    str = ll.ids;
    [self collextionshuaxin];
}
@end
